//
//  UIBarButtonItem+Extension.m
//  FastRunning
//
//  Created by YANG on 2018/4/16.
//  Copyright © 2018年 HUA. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

/** 左边只带 只带图片的ButtonItem */
+ (instancetype)itemWithLeftImage:(UIImage *)img highImage:(UIImage *)highImg target:(id)target action:(SEL)action {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:img forState:UIControlStateNormal];
    [button setImage:highImg forState:UIControlStateHighlighted];
    CGSize size = button.currentBackgroundImage.size;
    size.width = 60;
    size.height += 20;
    button.size = size;
    button.contentMode = UIViewContentModeLeft;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[self alloc]initWithCustomView:button];

}

/** 只带图片的ButtonItem */
+ (instancetype)itemWithImage:(NSString *)img highImage:(NSString *)highImg target:(id)target action:(SEL)action {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImg] forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    CGSize size = button.currentBackgroundImage.size;
    button.size = size;

    return [[self alloc]initWithCustomView:button];
}

/** 只有文字和高亮文字颜色的ButtonItem */
+ (instancetype)itemWithTitle:(NSString *)title color:(UIColor *)color highColor:(UIColor *)highColor target:(id)target action:(SEL)action {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:highColor forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    CGFloat w = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}].width;
    button.width = w < 40?40:w;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    button.contentEdgeInsets = UIEdgeInsetsMake(0, -7, 0, 0);
    return [[self alloc]initWithCustomView:button];

}

/** 只有文字和高亮文字颜色的RihtButtonItem */
+ (instancetype)itemWithRihtTitle:(NSString *)title color:(UIColor *)color highColor:(UIColor *)highColor target:(id)target action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:highColor forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIFont *font = [UIFont systemFontOfSize:16];
    CGFloat w = [title sizeWithAttributes:@{NSFontAttributeName:font}].width;
    button.width = w < 40?40:w;
    button.titleLabel.font = font;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -7)];
    return [[self alloc]initWithCustomView:button];
}

/** 带有选中文字和高亮文字颜色的RihtButtonItem */
+ (instancetype)itemWithRihtNormalTitle:(NSString *)title selectedTitle:(NSString *)selTitle color:(UIColor *)color highColor:(UIColor *)highColor target:(id)target action:(SEL)action {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:selTitle forState:UIControlStateSelected];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:highColor forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIFont *font = [UIFont systemFontOfSize:16];
    CGFloat w = [title sizeWithAttributes:@{NSFontAttributeName:font}].width;
    button.width = w < 40?40:w;
    button.titleLabel.font = font;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -7)];
    return [[self alloc]initWithCustomView:button];
}

@end
